package com.yl.chainStore.services.impl.pay

import com.yl.chainStore.entity.pay.PayCustomerOrder
import com.yl.chainStore.payrule.common.DiscountComputeOrderResultVo
import com.yl.chainStore.payrule.coupons.CouponsRule
import com.yl.chainStore.payrule.coupons.CouponsRuleService
import com.yl.chainStore.services.pay.IPayCouponsRuleService
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnSuccess
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class PayCouponsRuleServiceImpl(

) : IPayCouponsRuleService {

	companion object {
		private val log = LoggerFactory.getLogger(IPayCouponsRuleService::class.java)
	}

	@Autowired
	private lateinit var couponsRuleServices: List<CouponsRuleService>


	override fun completeOrder(order: PayCustomerOrder,
	                           isUse: Boolean,
	                           useId: Boolean,
	                           couponIds: List<Long>?): DiscountComputeOrderResultVo {
		// TODO: 2021/9/8 需修改模式
		log.info(
			"优惠卷优惠管理：==>: 执行订单：order:{}；isUse：{}；useId：{}；ids:{}", order,
			isUse, useId, couponIds
		)
		var resOrder = order
		for (ruleService in couponsRuleServices) {
			val computeOrder = ruleService.computeOrder(resOrder, isUse, useId, couponIds)
			if(useId && computeOrder.responseBodyIsError()){
				return computeOrder
			}
			resOrder = computeOrder.order
		}
		return DiscountComputeOrderResultVo(returnSuccess(),resOrder)
	}
}
